package cn.springbook.common.core.model;


import java.io.Serializable;

/**
 * <p>
 * 统一API响应结果封装
 * </p>
 *
 * @author: caifenglin
 * @date: 2021/6/24 19:40
 */
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回结果,默认为false
     */
    private boolean success;

    /**
     * 状态码
     */
    private int code;

    /**
     * 简单信息或消息
     */
    private String message;

    /**
     * 携带的参数
     */
    private T data;

    /**
     * 时间戳
     */
    private Long timestamp = System.currentTimeMillis();


    public ResponseResult() {
    }


    public ResponseResult(boolean success, int code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public static <T> ResponseResultBuilder<T> builder() {
        return new ResponseResultBuilder<T>();
    }

    public static final class ResponseResultBuilder<T> {

        private boolean success;

        private int code;

        private String message;

        private T data;

        private ResponseResultBuilder() {
        }

        public ResponseResult.ResponseResultBuilder<T> withSuccess(boolean success) {
            this.success = success;
            return this;
        }

        public ResponseResult.ResponseResultBuilder<T> withCode(int code) {
            this.code = code;
            return this;
        }

        public ResponseResult.ResponseResultBuilder<T> withMessage(String message) {
            this.message = message;
            return this;
        }

        public ResponseResult.ResponseResultBuilder<T> withData(T data) {
            this.data = data;
            return this;
        }

        /**
         * Build result.
         *
         * @return result
         */
        public ResponseResult<T> build() {
            ResponseResult<T> responseResult = new ResponseResult<T>();
            responseResult.setSuccess(success);
            responseResult.setCode(code);
            responseResult.setMessage(message);
            responseResult.setData(data);
            return responseResult;
        }
    }
}
